﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RouterControl.Whs.SingletonService;

namespace RouterControl.WhsAddIn.ConsoleControls
{
	public partial class RouterServiceNotRunning : ConsoleControlBase
	{
		public RouterServiceNotRunning(RouterControlConsoleTab2 tab)
			: base(tab)
		{
			if (_log.IsDebugEnabled)
				_log.Debug("Initialization start");

			try
			{
				InitializeComponent();
	
				this.lineBox.Header.Text = Properties.Resources.txtRouterControlServiceNotRunning;

				// done
				if (_log.IsDebugEnabled)
					_log.Debug("Initialization done");
			}
			catch (Exception e)
			{
				_log.Fatal("Failed to initialize", e);

				throw;
			}
		}

		private void startRouterControlServiceLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			StartWindowsService();
		}

		private void startRouterControlServiceBgWorker_DoWork(object sender, DoWorkEventArgs e)
		{
			try
			{
				using (new UserRecognizeWait(false))
				{
                    switch (_singletonService.IsWindowsServiceRunning())
                    {
                        case SingletonService2Factory.ServiceStatus.Running:
                            // everything is fine
                            break;

                        case SingletonService2Factory.ServiceStatus.NotInstalled:
                            return;

                        case SingletonService2Factory.ServiceStatus.Stopped:
                            _singletonService.StartWindowsService();
                            break;

                        default:
                            throw new Exception("unknown value");
                    }
				}
			}
			catch (Exception ex)
			{
				_log.Error("Failed to start Router Control service", ex);

				Microsoft.HomeServer.Controls.QMessageBox.Show(
					ex.Message,
					Properties.Resources.txtRouterControlServiceStartFailed,
					MessageBoxButtons.OK,
					MessageBoxIcon.Error
					);
			}
		}

		private void startRouterControlServiceBgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
            switch (_singletonService.IsWindowsServiceRunning())
            {
                case SingletonService2Factory.ServiceStatus.Running:
                    // everything is fine
                    _tab.SetState(
                        _singletonService.Channel.IsDeviceSelected() ?
                        RouterControlConsoleTab2.RouterState.ConnectingRouter :
                        RouterControlConsoleTab2.RouterState.NoRouterSelected
                        );
                    break;

                case SingletonService2Factory.ServiceStatus.NotInstalled:
                    this.statusImage.Image = Properties.Resources.imgError;
                    this.routerControlServiceStatusLabel.Text = Properties.Resources.txtRouterControlServiceStatusNotInstalled;
                    this.startRouterControlServiceLink.Visible = false;
                    this.startRouterControlServiceLink.Enabled = false;
                    return;

                case SingletonService2Factory.ServiceStatus.Stopped:
                    this.statusImage.Image = Properties.Resources.imgError;
                    this.routerControlServiceStatusLabel.Text = Properties.Resources.txtRouterControlServiceStatusStopped;
                    this.startRouterControlServiceLink.Visible = true;
                    this.startRouterControlServiceLink.Enabled = true;
                    break;

                default:
                    throw new Exception("unknown value");
            }
		}

		private void StartWindowsService()
		{
			if (_log.IsDebugEnabled)
				_log.Debug("Starting Router Control singleton service...");

			// start service
            this.statusImage.Image = Properties.Resources.imgProgressAnim;
			this.startRouterControlServiceLink.Enabled = false;
			this.routerControlServiceStatusLabel.Text = Properties.Resources.txtRouterControlServiceStatusStarting;

			this.startRouterControlServiceBgWorker.RunWorkerAsync();
		}

		private void RouterServiceNotRunning_Load(object sender, EventArgs e)
		{
			StartWindowsService();
		}
	}
}
